package com.tengju.bff.interfaces.openapi.facade;

import com.tengju.bff.interfaces.covercharge.service.LeTaxOneService;
import com.tengju.bff.interfaces.openapi.vo.IdentityAuthRequest;
import com.tengju.bff.interfaces.openapi.vo.LetaxResponse;
import com.tengju.bff.interfaces.openapi.vo.RegResultNotifyRequest;
import com.tengju.settlement.domain.model.account.Account;
import com.tengju.settlement.domain.model.account.AccountRepository;
import com.tengju.user.domain.model.archive.letax.LetaxAccount;
import com.tengju.user.domain.model.archive.letax.LetaxRepository;
import com.tengju.user.domain.model.archive.letax.LetaxSnapshot;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.model.user.UserInfo;
import com.tengju.user.domain.model.user.UserInfoRepository;
import com.tengju.user.domain.shared.AccountId;
import com.tengju.user.domain.shared.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.tengju.settlement.domain.model.account.AccountType.DATA_TREE;

@Slf4j
@Component
public class LetaxFacade {

    private static final String AES_SEED = "DcServic";

    private final UserInfoRepository userInfoRepository;

    private final AccountRepository accountRepository;

    private final LetaxRepository letaxRepository;

    private final LeTaxOneService leTaxOneService;

    @Autowired
    public LetaxFacade(UserInfoRepository userInfoRepository,
                       AccountRepository accountRepository,
                       LetaxRepository letaxRepository,
            LeTaxOneService leTaxOneService) {
        this.userInfoRepository = userInfoRepository;
        this.accountRepository = accountRepository;
        this.letaxRepository = letaxRepository;
        this.leTaxOneService = leTaxOneService;
    }

    public LetaxResponse identityAuth(IdentityAuthRequest data) {

        if (data == null || data.getBody() == null) {
            return LetaxResponse.error("请求失败，参数为空");
        }

        IdentityAuthRequest.Body body = data.getBody();
        String infoStr = body.getCoreCompanyMerchantId();
        if (StringUtils.isEmpty(infoStr)) {
            return LetaxResponse.error("请求失败，中心企业商户编码参数为空");
        }

        String[] infos = infoStr.split("_");
        UserIdCode idCode = new UserIdCode(Long.parseLong(infos[0]));
        String ticket = body.getTicket();


        //验证中心企业商户编码

        UserInfo user = userInfoRepository.getByIdCode(idCode);
        if (user == null) {
            return LetaxResponse.error("不存在该商户");
        }

        Account account = accountRepository.find(new com.tengju.settlement.domain.model.account.UserIdCode(idCode.getValue()),
                DATA_TREE);
        if (account == null) {
            return LetaxResponse.error("该商户没有注册账户");
        }

        LetaxAccount letaxAccount = letaxRepository.find(idCode, new AccountId(account.getAccountId().getValue()));
        if (letaxAccount == null) {
            return LetaxResponse.error("该商户没有在app上进行乐税收款账户的注册");
        }

        return LetaxResponse.builder()
                .head(LetaxResponse.Head.builder()
                        .rtnCode("00")
                        .rtnMsg("请求成功")
                        .build())
                .body(LetaxResponse.Body.builder()
                        .bizCode(StringUtils.isEmpty(ticket) ? "00" : "10")
                        .bizMsg("认证成功")
                        .merchantInfo(LetaxResponse.MerchantInfo.builder()
                                .name(letaxAccount.getIdCardName())
                                .idCardNo(letaxAccount.getIdCardNo())
                                .build())
                        .build())
                .build();
    }

    public LetaxResponse regResultNotify(RegResultNotifyRequest data) {

        try {
            if (data == null || data.getBody() == null) {
                return LetaxResponse.error("请求失败，参数为空");
            }

//            RegResultNotifyRequest.Body body = data.getBody();
//            String infoStr = body.getCoreCompanyMerchantId();
//            if (StringUtils.isEmpty(infoStr)) {
//                return LetaxResponse.error("请求失败，中心企业商户编码参数为空");
//            }
//
//
//            LetaxSnapshot merchantInfo = body.getMerchantInfo();
//            String[] infos = infoStr.split("_");
//            UserIdCode idCode = new UserIdCode(Long.parseLong(infos[0]));
//
//            log.info("乐税工商注册完成异步通知代理ID_CODE：{}", merchantInfo.getIdCode());
//
//            Account account = accountRepository.find(new com.tengju.settlement.domain.model.account.UserIdCode(idCode.getValue()),
//                    DATA_TREE);
//            if (account == null) {
//                return LetaxResponse.error("请求失败，该商户没有注册服务费账户");
//            }
//
//            //判断注册用户是否存在
//            LetaxAccount letaxAccount = letaxRepository.find(idCode, new AccountId(account.getAccountId().getValue()));
//            if (letaxAccount == null || letaxAccount.isTerminated()) {
//                throw new BizException("该商户没有在app上进行乐税收款账户的注册");
//            }
//
//
//
//            leTaxOneService.register(idCode, new AccountId(account.getAccountId().getValue()),
//                    letaxAccount, merchantInfo);

            return LetaxResponse.builder()
                    .head(LetaxResponse.Head.builder()
                            .rtnCode("00")
                            .rtnMsg("请求成功")
                            .build())
                    .build();
        } catch (BizException e) {
            log.error("乐税工商注册完成异步失败", e);
            return LetaxResponse.error(e.getMessage());
        } catch (Exception e) {
            log.error("乐税工商注册完成异步异常", e);
            return LetaxResponse.error("乐税工商注册完成异步失败：" + e.getMessage());
        }
    }
}
